﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using ifunction.SqlDeployConsole.Core;
using System.Threading;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
using ifunction.SqlDeployConsole.Properties;

namespace ifunction.SqlDeployConsole
{
    /// <summary>
    /// 
    /// </summary>
    public static class ConfigurationHelper
    {
        private const string xmlNode_Solution = "Solution";

        private const string xmlNode_Database = "Database";

        private const string xmlAttribute_Name = "Name";

        /// <summary>
        /// Loads the solutions.
        /// </summary>
        /// <param name="xDocument">The x document.</param>
        /// <returns></returns>
        public static List<Solution> LoadSolutions(XDocument xDocument)
        {
            List<Solution> solutions = new List<Solution>();

            if (xDocument != null && xDocument.Root.Name.LocalName == xmlNode_Database)
            {
                bool isOldVersion = xDocument.Root.Elements().Count() > 0 && xDocument.Root.Element(xmlNode_Solution) == null;
                if (isOldVersion)
                {
                    AdjustToNewVersion(xDocument);
                }
            }

            return solutions;
        }

        /// <summary>
        /// Adjusts to new version.
        /// </summary>
        /// <param name="xDocument">The x document.</param>
        private static void AdjustToNewVersion(XDocument xDocument)
        {
            if (xDocument != null)
            {
                XElement xElement = CreateNodeByName(xmlNode_Solution);
                xElement.SetAttributeValue(xmlAttribute_Name, "Default");

                foreach (var one in xDocument.Root.Elements())
                {
                    one.Remove();
                    xElement.Add(one);
                }

                xDocument.Root.Add(xElement);
            }
        }

        /// <summary>
        /// Loads the solution.
        /// </summary>
        /// <param name="node">The node.</param>
        /// <returns></returns>
        private static Solution LoadSolution(XElement node)
        {
            Solution solution = null;

            if (node != null)
            {
                solution = new Solution();


            }

            return solution;
        }

        /// <summary>
        /// Creates the name of the node by.
        /// </summary>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        private static XElement CreateNodeByName(this string name)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("Name is null or empty.");
            }

            try
            {
                return XElement.Parse(string.Format("<{0}></{0}>", name));
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Failed to create node by name.", ex);
            }
        }
    }
}
